package org.myapp.page.login.form;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.validation.validator.StringValidator;

import org.myapp.datastore.model.UserModel;
import org.myapp.page.validator.AlphaNurmericValidator;

/**
 * パスワード変更フォーム.<br>
 *
 * @author Administrator
 *
 */
public class ChangeForm extends BaseForm<LoginFormModel> {

    private static final long serialVersionUID = 9028317115603380090L;

    private final PasswordTextField newPassword1 = new PasswordTextField("txtNewPassword1");
    private final PasswordTextField newPassword2 = new PasswordTextField("txtNewPassword2");

    /**
     * コンストラクタ.<br>
     *
     */
    public ChangeForm(LoginFormModel model) {
        super("psChangeForm", model);

        // Validator
        newPassword1.add(new AlphaNurmericValidator());
        newPassword2.add(new AlphaNurmericValidator());
        newPassword1.add(StringValidator.minimumLength(6));
        newPassword2.add(StringValidator.minimumLength(6));

        add(newPassword1, newPassword2);

        // FormValidator(相関)
        add(new AbstractFormValidator() {

            private static final long serialVersionUID = 1L;

            @Override
            public void validate(Form<?> arg0) {

                String userId = getUserId().getConvertedInput();
                String password = getPassword().getConvertedInput();
                String newPassword1 = getNewPassword1().getConvertedInput();
                String newPassword2 = getNewPassword2().getConvertedInput();

                // ユーザの存在を確認
                if (getLoginService().getUser(userId, password) == null ) {
                    error(getUserId(), "psChangeForm.existUserError");
                }

                // パスワードの一致
                if (!newPassword1.equals(newPassword2)) {
                    error(getNewPassword1(), "psChangeForm.equalPassError");
                }
            }

            @Override
            public FormComponent<?>[] getDependentFormComponents() {
                return new FormComponent[] {getUserId(), getPassword(), getNewPassword1(), getNewPassword2()};
            }
        });

        add(new Button("btnPsChange") {

            private static final long serialVersionUID = -9058266945309241249L;

            @Override
            public void onSubmit() {

                LoginFormModel formModel = getFormModel();

                UserModel userModel = getLoginService().getUser(formModel.getTxtUserId(), formModel.getTxtPassword1());

                userModel.setPassword(formModel.getTxtNewPassword1());

                userModel.save();

                info(this.getLocalizer().getString("msg.info.3", getPage()));
            }
        });
    }

    public PasswordTextField getNewPassword1() {
        return newPassword1;
    }

    public PasswordTextField getNewPassword2() {
        return newPassword2;
    }
}
